from launch import LaunchDescription
from launch.actions import ExecuteProcess, DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
import os
from ament_index_python.packages import get_package_share_directory

def generate_launch_description():
    # Get the package share directory
    pkg_share = get_package_share_directory('http_server_launch')
    
    # Define HTML file directory - use the html directory in our package
    default_html_dir = os.path.join(pkg_share, 'html')

    html_dir_arg = DeclareLaunchArgument(
        'html_dir',
        default_value=default_html_dir,
        description='HTML directory to serve'
    )
    
    # Create launch configuration for port
    port_arg = DeclareLaunchArgument(
        'port',
        default_value='80',
        description='Port to run the HTTP server on'
    )
    
    # Using python -m http.server to start the HTTP server
    http_server = ExecuteProcess(
        cmd=['python3', '-m', 'http.server', LaunchConfiguration('port'), '--directory', LaunchConfiguration('html_dir')],
        output='screen',
        emulate_tty=True
    )

    return LaunchDescription([
        port_arg,
        html_dir_arg,
        http_server
    ])